<template>
  <div class="app-container">
    <div class="question-wrapper">
      <div class="header">
        <h3>垃圾分类测试题</h3>
        <span @click="exChange" class="change">换一批</span>
      </div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <template v-for="(item, index) in ruleForm.questions">
          <el-form-item :key="item.id" :prop="ruleForm.questions[index].answer">
            <h3>{{index + 1}}.{{item.content}}</h3>
            <el-radio-group v-model="ruleForm.questions[index].answer">
              <el-radio label="A">A.{{item.A}}</el-radio>
              <el-radio label="B">B.{{item.B}}</el-radio>
              <el-radio label="C">C.{{item.C}}</el-radio>
              <el-radio label="D">D.{{item.D}}</el-radio>
            </el-radio-group>
            <div v-show="showDesc">
              <p :class="ruleForm.questions[index].answer === ruleForm.questions[index].correct ? 'green' : 'red'">{{ruleForm.questions[index].answer === ruleForm.questions[index].correct ? '正确' : '错误'}}</p>
              <p>答案：{{item.correct}}</p>
              <p>解析：{{item.describe}}</p>
            </div>
          </el-form-item>
        </template>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')" :disabled="isSubmit">立即提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: "Question",
  data() {
    return {
      questions: [],
      ruleForm: {
        questions: []
      },
      rules: {
        answer: [
          { required: true, message: "请选择选项", trigger: "change" }
        ],
      },
      rules2: {
        resource: [
          { required: true, message: "请选择选项", trigger: "change" }
        ],
      },
      showDesc: false,
      isSubmit: false,
      rightCount: 0,
      totalSize: 0
    };
  },

  mounted() {
    this.getQuestions();
  },

  methods: {
    exChange() {
      this.getQuestions()
    },
    async getQuestions() {
      const res = await this.$http.get("/users/questions");
      this.ruleForm.questions = res.data;
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let count = 0
          let size = 0
          this.ruleForm.questions.forEach(item => {
            if(item.answer === item.correct) {
              count += 1
              size += item.add_count
            }
          })
          this.$alert(`您本次答题共答对${count}道,共得分${size}分`, {
          confirmButtonText: '确定',
          callback: action => {
            // this.$message({
            //   type: 'info',
            //   message: `action: ${ action }`
            // });
            console.log(action)
          }
        });
          this.rightCount = count
          this.totalSize = size
          this.showDesc = true
          this.isSubmit = true
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.isSubmit = false
    }
  }
};
</script>
<style lang="less" scoped>
  @import '../assets/style/question.less';
</style>